//{=====================================================
//! @file    StackCalculator.cpp
//! @date    2014-10-10
//! @author  Artem Mikhalev <grozzmaster@gmail.com>
//}=====================================================

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>

#include "mystack.h"
//{=====================================================
//! In the file 'mystack.h' you can find descriptions of all functions
//}=====================================================


int main ()
{
    printf ("# -----------------------------------------\n");
    printf ("# StackCalculator v.1.0 " __DATE__ " " __TIME__ "\n");
    printf ("# -----------------------------------------\n");

    const char* ptr_str_push = "push\0";
    const char* ptr_str_pop = "pop\0";
    const char* ptr_str_sum = "add\0";
    const char* ptr_str_mul = "mul\0";
    const char* ptr_str_sub = "sub\0";
    const char* ptr_str_div = "div\0";
    const char* ptr_str_end = "end\0";

    printf ("# Commands:\n");
    printf ("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", ptr_str_push, ptr_str_pop, ptr_str_sum, ptr_str_mul, ptr_str_sub, ptr_str_div, ptr_str_end);
    printf ("# -----------------------------------------\n");

    char* str = (char*) calloc (100, sizeof (char));
    assert (str);
    for (int i = 0; i < 100; i++) *(str + i) = 0;

    /*Constructing of stack*/
    struct mystack st;
    st.arr = (double*) calloc (100, sizeof (double));
    for (int i = 0; i < 100; i++) *(st.arr + i) = -100;
    //assert (stackOK (&st));
    /*End constructing of stack*/

    int ret = 0;
    while (ret != -1) //Only function 'destruct' returnes '-1'
    {
        scanf ("%s", str);

        ret = WRONG_COMMAND; /*If input command doesn't exist, ret will be 3 (const int WRONG_COMMAND  = 3)*/
        if (strcmp (str, ptr_str_push) == 0) ret = push (&st);
        if (strcmp (str, ptr_str_pop) == 0) ret = pop (&st);
        if (strcmp (str, ptr_str_sum) == 0) ret = add (&st);
        if (strcmp (str, ptr_str_mul) == 0) ret = mul (&st);
        if (strcmp (str, ptr_str_sub) == 0) ret = sub (&st);
        if (strcmp (str, ptr_str_div) == 0) ret = div (&st);

        assert (!stackOK (&st));
        if (strcmp (str, ptr_str_end) == 0) ret = -1;

        switch (ret)
        {
            case TOO_MANY:      printf ("ERROR. TOO MANY NUMBERS IN STACK!\n");   ret = -1; break;
            case TOO_FEW:       printf ("ERROR. TOO FEW NUMBERS IN STACK!\n");    ret = -1; break;
            case WRONG_COMMAND: printf ("ERROR. INPUT COMMAND DOESN'T EXIST!\n"); ret = -1; break;
            case DIVISION_ZERO: printf ("ERROR. DIVISION BY ZERO!\n");            ret = -1; break;
            case BAD_STACK:     printf ("ERROR. THE STACK IS BAD!\n");            ret = -1; break;
            case HAPPINESS:     break;
            default:            printf ("WRONG VALUE OF ret. IT IS %d\n", ret);  ret = -1; break;
        }

        if (ret == -1) ret = destruct (&st);
    }

    free (str);
    str = 0;

    return 0;
}
